#include<stdio.h> 
#include<stdlib.h>
#include<unistd.h> 
#include<string.h>
#include<sys/types.h>
#include<fcntl.h>
int main()
{
	close(0);
	int fd = open("log.txt", O_RDONLY | O_CREAT, 0664);
	if (fd < 0){
		perror("open");
		return 1;
	}
	char str[40];
	while (scanf("%s", str) != EOF){
		printf("%s\n", str);
	}
	close(fd);
	return 0;
}

int out1()
{
  close(1);
	int fd = open("log.txt", O_WRONLY | O_CREAT, 0666);
	if (fd < 0){
		perror("open");
		return 1;
	}
	printf("hello world\n");
	printf("hello world\n");
	printf("hello world\n");
	printf("hello world\n");
	printf("hello world\n");
	fflush(stdout);
	
	close(fd);
}


int fun1()//向文件写入
{

  int fd = open("./log.txt", O_WRONLY | O_CREAT, 0644);
  if(fd < 0){
    perror("open");
    return 1;
  }
  const char* msg = "hello file!\n";
  int cnt = 5;
  while(cnt--){
    write(fd,msg,strlen(msg));
    // 写入的过程中不需要加上\0
    // \0作为字符串的结尾只是C语言的规定
  }
  close(fd);
  return 0;
}

int fun2()//从文件读取
{

  int fd = open("./log.txt", O_RDONLY);
  if(fd<0){
    perror("open");
    return 1;
  }
  char buffer[1024];
  ssize_t s = read(fd,buffer,sizeof(buffer)-1);
  if(s>0){
    buffer[s] = 0;
    printf("%s\n",buffer);
  }
  close(fd);
  return 0;
}
